<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Event_m extends CI_Model {
        
        var $config;
        
        function __construct() {
            parent::__construct();
            $this->config = array(
                'start_day' => 'monday',
                'show_next_prev' => true,
                'next_prev_url' => site_url().'events/calendar',
            );
             
            $this->config['template'] = '
                {table_open}<table border="0" cellpadding="0" cellspacing="0" class="calendar" style="min-width:500px">{/table_open}

                {heading_row_start}<tr>{/heading_row_start}

                {heading_previous_cell}<th><a href="{previous_url}" class="btn btn-default" style="margin-bottom:1em;"><span class="glyphicon glyphicon-chevron-left"></span></a></th>{/heading_previous_cell}
                {heading_title_cell}<th colspan="{colspan}" class="month-text text-center">{heading}</th>{/heading_title_cell}
                {heading_next_cell}<th><a href="{next_url}" class="btn btn-default pull-right" style="margin-bottom:1em;"><span class="glyphicon glyphicon-chevron-right"></span></a></th>{/heading_next_cell}

                {heading_row_end}</tr>{/heading_row_end}

                {week_row_start}<tr>{/week_row_start}
                {week_day_cell}<td class="week-day" style="border:solid 1px gray;">{week_day}</td>{/week_day_cell}
                {week_row_end}</tr>{/week_row_end}

                {cal_row_start}<tr class="days">{/cal_row_start}
                {cal_cell_start}<td class="day">{/cal_cell_start}

                {cal_cell_content}
                    <div class="day_num">{day}</div>
                        <ul class="content">{content}</ul>
                    </a>
                {/cal_cell_content}
                {cal_cell_content_today}
                    <div class="day_num highlight">{day}</div>
                    <div class="content">{content}</div>
                {/cal_cell_content_today}

                {cal_cell_no_content}<div class="day_num">{day}</div>{/cal_cell_no_content}
                {cal_cell_no_content_today}<div class="day_num highlight">{day}</div>{/cal_cell_no_content_today}

                {cal_cell_blank}&nbsp;{/cal_cell_blank}

                {cal_cell_end}</td>{/cal_cell_end}
                {cal_row_end}</tr>{/cal_row_end}

                {table_close}</table>{/table_close}
            '; 
        }
        
        function generate_calendar($year, $month){
            
            $this->load->library('calendar', $this->config); 
            
            $cal_data = $this->get_calendar_data($year, $month);
            
            return $this->calendar->generate($year, $month, $cal_data);   
        }
        
        function get_calendar_data($year, $month) {
            $q = $this->db->select('start_date, event_id, event_name, event_status')->from('event')
                    ->like('start_date', "$year-$month", 'after')->get();
            $cal_data = array();
            foreach ($q->result() as $row) {
                $index = ltrim(substr($row->start_date,8,2), '0');   // this is the magic !!
                $cal_data[$index][$row->event_id]['title'] = $row->event_name;
                $cal_data[$index][$row->event_id]['id'] = $row->event_id;
                $cal_data[$index][$row->event_id]['status'] = $row->event_status;
                //$cal_data[substr($row->start_date,8,2)] = $row->event_name; 
                //$cal_data['event_id'] = ;
            }
            return $cal_data;
        }
        
        function get_event($event_id){
            $q = $this->db->select('*')->from('event')->join('event_type', 'event.type_id=event_type.type_id')
                    ->where('event.event_id', $event_id)->get();
            if($q->num_rows() > 0) {
                return $q->row();
            }else{
                return FALSE;
            }
        } 
        
        function get_client($event_id){
            $q = $this->db->where('event_id', $event_id)->get('client');
            if($q->num_rows()>0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function get_event_coord($event_id){
            $q = $this->db->select('*')->from('event_coordinator')
                    ->join('coordinator', 'event_coordinator.coordinator_id=coordinator.coordinator_id')
                    ->where('event_coordinator.event_id', $event_id)->get();
            if($q->num_rows()>0){
                return $q->row();
            }else{
                return FALSE;
            }
            
        }
        
        function get_event_venue($event_id){
            $q = $this->db->select('*')->from('venue')->join('event_venue', 'venue.venue_id=event_venue.venue_id')
                    ->where('event_id', $event_id)->get();
            if($q->num_rows()>0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_event_supplier($event_id){
            $q = $this->db->select('*')->from('event')
                    ->join('event_supplier', 'event.event_id=event_supplier.event_id')
                    ->join('supplier', 'event_supplier.supplier_id=supplier.supplier_id')
                    ->where('event.event_id', $event_id)->get();
            if($q->num_rows()>0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function update_event_services($event_id, $supplier_id,$services){
            $this->db->set('event_svc', $services)->where('supplier_id', $supplier_id)->where('event_id', $event_id)
                    ->update('event_supplier');
            return $this->db->affected_rows();
        }
        
        function get_event_checklist($event_id){
            $q = $this->db->select('*')->from('event_checklist')->join('checklist', 
                    'event_checklist.checklist_id=checklist.checklist_id')
                    ->join('category', 'checklist.category_id=category.category_id')
                    ->where('event_id', $event_id)->get();
            if($q->num_rows()>0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_events($id){
            $sql = "SELECT * FROM event";
            $q = $this->db->query($sql, $id);
            if($q->num_rows() > 0) {
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        function get_graph($id){
            $sql = "SELECT type_id FROM event";
            $q = $this->db->query($sql, $id);
            if($q ->num_rows()>0){
                
                return $q->result_array();
          
            }else{
                return FALSE;
            }
            
        }
        
        function get_event_types() {
            $q = $this->db->get('event_type');
            if($q->num_rows() > 0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_event_type($id) {
            $q = $this->db->where('type_id', $id)->get('event_type');
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function get_services($id){
            $q = $this->db->select('services,supplier_id')->from('supplier')->where('supplier_id', $id)->get();
            if($q->num_rows > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        function check_if_type_exist($type_name){
            $q = $this->db->where('type_name', $type_name)->get('event_type');
            return $q->num_rows();
        }
        function insert_event_type($type_name) {
            $this->db->insert('event_type', array('type_name' => $type_name));
            return $this->db->insert_id();
        }
        
        function update_event_type($type_id, $type_name) {
            $this->db->where('type_id', $type_id)->update('event_type', array('type_name' => $type_name));
        }
        
        function delete_event_type($type_id) {
            $num = '';
            for($i=0;$i<sizeof($type_id);$i++){
                $q = $this->db->where('type_id', $type_id[$i])->delete('event_type');
                if(!$q){
                    $num = $this->db->_error_number();
                }else{
                    $num = '';
                }
            }
            return $num;
        }
            
        function get_venues() {
            $q = $this->db->get('venue');
            if($q->num_rows() > 0){
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_venue($id) {
            $q = $this->db->where('venue_id', $id)->get('venue');
            if($q->num_rows() > 0){
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function check_if_venue_exist($venue_name, $venue_address){
            $q = $this->db->where('venue_name', $venue_name)->where('venue_address', $venue_address)
                    ->get('venue');
            return $q->num_rows();
        }
        
        function insert_venue($data) {
            $this->db->insert('venue', $data);
            return $this->db->insert_id();
        }
        
        function update_venue($venue_id, $data){
            $this->db->where('venue_id', $venue_id)->update('venue', $data);
            return $this->db->affected_rows();
        }
        
        function delete_venue($venue_id) {
            $num = '';
            for($i=0;$i<sizeof($venue_id);$i++){
                $q = $this->db->where('venue_id', $venue_id[$i])->delete('venue');
                if(!$q){
                    $num = $this->db->_error_number();
                }else{
                    $num = '';
                }
            }
            return $num;
        }
        
        function terminate_event($event_id){
            $this->db->set('event_status', 1)->where('event_id', $event_id)->update('event');
            return $this->db->affected_rows();
        }
        
        function cancel_event($event_id){
            $this->db->set('event_status', 2)->where('event_id', $event_id)->update('event');
            return $this->db->affected_rows();
        }
        
        function insert_event($data, $venue_id, $coord_id, $client_id, $event_checklist, 
                $event_supplier, $event_task, $user){
            $this->db->insert('event', $data);
            $event_id = $this->db->insert_id();
            $affected_rows = $this->db->affected_rows();
            $data = explode(',', $event_checklist);
            if($coord_id!=''){
                $this->db->set('coordinator_id', $coord_id)
                        ->set('event_id', $event_id)
                        ->insert('event_coordinator');
            }
            if($client_id!=''){
                $this->db->set('event_id', $event_id)->where('client_id', $client_id)
                        ->update('client');
            }
            if($venue_id!=''){
                for($i=0;$i<sizeof($venue_id);$i++){
                    $this->db->set('venue_id', $venue_id[$i])
                            ->set('event_id', $event_id)->insert('event_venue');
                }
            }
            if($event_supplier!=''){
                for($i=0;$i<sizeof($event_supplier);$i++){
                    $supplier = explode('-', $event_supplier[$i]);
                    $this->db->set('supplier_id', $supplier[0])->set('event_id', $event_id)
                            ->set('event_svc', $supplier[1])->insert('event_supplier');
                }
            }
            if($event_checklist!=''){
                for($i=0;$i<sizeof($data);$i++){
                    $this->db->set('checklist_id', $data[$i])->set('event_id', $event_id)
                            ->insert('event_checklist');
                }
            }
            if($event_task!=''){
                for($i=0;$i<sizeof($event_task);$i++){
                    $task = explode(';', $event_task[$i]);
                    $this->db->set('task_desc', $task[0])->set('task_date', $task[1])
                            ->set('task_time', $task[2])->set('coord_id', $user)->insert('task');
                }
            }
            return $affected_rows;
        }
        
        function update_event_details($event_id, $event_data, $coord_id, $client_id, $venue_id){
            $row = 0;
            $this->db->where('event_id', $event_id)->update('event', $event_data);
            $row+=$this->db->affected_rows();
            if(sizeof($venue_id)==0){
                $this->db->where('event_id', $event_id)->delete('event_venue');
                $row+=$this->db->affected_rows();
            }else{
                for($i=0;$i<sizeof($venue_id);$i++){
                    $this->db->where('event_id', $event_id)->set('venue_id', $venue_id[$i])
                            ->update('event_venue');
                    $row+=$this->db->affected_rows();
                }
            }
            $this->db->where('event_id', $event_id)->set('coordinator_id', $coord_id)
                    ->update('event_coordinator');
            $row+=$this->db->affected_rows();
            return $row;
            
        }
        function insert_event_checklist($data){
            $this->db->insert('event_checklist', $data);
            return $this->db->affected_rows();
        }
        function update_event_checklist($event_id, $checklist){
            $ctr = 0;
            for($i=0;$i<sizeof($checklist);$i++){
                $item = explode('-', $checklist[$i]);
                $this->db->set('checklist_status', $item[1])->set('ch_remarks', $item[2])
                        ->where('event_id',$event_id)->where('checklist_id', $item[0])
                        ->update('event_checklist');
                if($this->db->affected_rows()>0){
                    $ctr++;
                }
            }
            return $ctr;
        }
        function add_event_supplier($data){
            $this->db->insert('event_supplier', $data);
            return $this->db->affected_rows();
        }
        
        function remove_event_supplier($id){
            $this->db->where('supplier_id', $id)->delete('event_supplier');
            return $this->db->affected_rows();
        }
            
        function get_max_main_layout_id(){
            $this->db->select(" main_layout_id FROM venue_main_layout ORDER BY main_layout_id DESC LIMIT 1");
            return $this->db->get();
        }
        
        function save_main_layout($id,$imgData,$event_id,$img_url){
            $this->db->set("main_layout_id",$id);
            $this->db->set("main_layout_data",$imgData);
            $this->db->set("main_img_url",$img_url);
            $this->db->set("event_id",$event_id);
            $this->db->insert("venue_main_layout");
        }
        
        function save_sub_lay($sub_id,$subData,$main_id){
            $this->db->set("sub_layout_id",$sub_id);
            $this->db->set("sub_layout_data",$subData);
            $this->db->set("main_layout_id",$main_id);
            $this->db->insert("venue_sub_layout");
        }
        
        function update_sub_lay($sub_id,$subData,$main_id){
            $this->db->where("sub_layout_id",$sub_id);
            $this->db->where("main_layout_id",$main_id);
            $this->db->update("venue_sub_layout",array('sub_layout_data' => $subData));
        }
        
        function save_main_bg($id,$url){
            $this->db->where("main_layout_id",$id);
            $this->db->update("venue_main_layout", array('main_img_url' => $url));
        }
        
        function update_main_layout($id,$imgData){
            $this->db->where("main_layout_id",$id);
            $this->db->update("venue_main_layout", array('main_layout_data' => $imgData));
        }
        
        function has_layout($id){
            $this->db->select("* FROM venue_main_layout");
            $this->db->where("event_id",$id);
            $query = $this->db->get();
            if ($query->num_rows() > 0){
               return true;
            }else{
               return false;
            }
        }
        
        function sub_has_layout($sub_id,$main_id){
            $this->db->select("* FROM venue_sub_layout");
            $this->db->where("sub_layout_id",$sub_id);
            $this->db->where("main_layout_id",$main_id);
            $query = $this->db->get();
            if ($query->num_rows() > 0){
               return true;
            }else{
               return false;
            }
        }
        
        function has_sub_layout($id){
            $this->db->where("main_layout_id",$id);
            $query = $this->db->get('venue_sub_layout');
            if ($query->num_rows() > 0){
                $row = $query->row();
                return $row->sub_layout_id;
            }else{
               return false;
            }
        }
        
        function get_main_layout_id($id){
            $this->db->select("* FROM venue_main_layout");
            $this->db->where("event_id",$id);
            return $query = $this->db->get();
        }
        
        function get_layout($id){
            $this->db->select("* FROM venue_main_layout");
            $this->db->where("event_id",$id);
            return $this->db->get();
        }
        
        function get_sub_layout($sub_id,$main_id){
            $this->db->select("* FROM venue_sub_layout");
            $this->db->where("sub_layout_id",$sub_id);
            $this->db->where("main_layout_id",$main_id);
            return $this->db->get();
        }
        
        function get_drag_category(){
            $this->db->select("* FROM drag_category");
            return $this->db->get();
            //return $res;
        }
        
        function get_drag_img($id){
            $this->db->select("* FROM drag_category JOIN drag_image USING(drag_category_id)");
            $this->db->where("drag_category_id",$id);
            return $this->db->get();
        }
       
        /**Production Schedule**/
        function insert_prodsched($prod,$pid){
            $this->db->insert('production_sched', array('description' => $prod, 'event_id' => $pid));
            
            $this->db->select('description FROM production_sched');
            $this->db->where('event_id', $pid);
            return $this->db->get();
        }
        
        function update_prodsched($produ, $prod_id){           	
            $this->db->where('event_id',$prod_id);
            $this->db->update('production_sched', array('description' => $produ));
            
            $this->db->select('description FROM production_sched');
            $this->db->where('event_id', $prod_id);
            return $this->db->get();
            
        } 
        function get_prodsched($event_id){
            $this->db->select('description FROM production_sched');
            $this->db->where('event_id', $event_id);
            return $this->db->get();
        }
        
        function check_floor_count($id){
            $this->db->where("event_id",$id);
            return $this->db->get("venue_main_layout");
            
        }
    }